package com.zhipu.balance.ui.vm.home.calculate.rice

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordRice
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 黄粒米率
 */
class RiceYellowGrainViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 稻谷实体
     */
    var mRecordRice : RecordRice? = null

    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 黄粒米-试样质量
     */
    var yellowGrainRiceSampleQuality = ObservableField("")
    /**
     * 黄粒米质量
     */
    var yellowGrainRiceQuality = ObservableField("")
    /**
     * 黄粒米率
     */
    var yellowGrainRiceResult = ObservableField("")




    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordRice?.yellowGrainRiceRecorder)){
            //黄粒米-试样质量
            yellowGrainRiceSampleQuality.set(mRecordRice?.yellowGrainRiceSampleQuality.toString())
            //黄粒米质量
            yellowGrainRiceQuality.set(mRecordRice?.yellowGrainRiceQuality.toString())
            //黄粒米率
            yellowGrainRiceResult.set(mRecordRice?.yellowGrainRiceResult.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //黄粒米-试样质量
        yellowGrainRiceSampleQuality.set("")
        //黄粒米质量
        yellowGrainRiceQuality.set("")
        //黄粒米率
        yellowGrainRiceResult.set("")
    }





    /**
     * 试样质量
     */
    fun BulkQuality(){
        yellowGrainRiceSampleQuality.set(weighing)
        mRecordRice?.yellowGrainRiceSampleQuality = yellowGrainRiceSampleQuality?.get()?.toDouble()
    }

    /**
     * 黄粒米质量
     */
    fun YellowGrainRiceQuality(){
        yellowGrainRiceQuality.set(weighing)
        mRecordRice?.yellowGrainRiceQuality = yellowGrainRiceQuality.get()?.toDouble()
    }





    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算整精米率
         */
        var mYellowGrainRiceResult = CommonUtils.formatDouble(mRecordRice!!.yellowGrainRiceQuality / mRecordRice!!.yellowGrainRiceSampleQuality * 100)
        yellowGrainRiceResult.set(mYellowGrainRiceResult.toString())
        mRecordRice?.yellowGrainRiceResult = mYellowGrainRiceResult


        /**
         * 黄粒米记录员
         */
        mRecordRice!!.yellowGrainRiceRecorder = mPersonnel!!.name



        if(mRecordRice?.yellowGrainRiceSampleQuality == 0.00){
            yellowGrainRiceSampleQuality.set("0.00")
        }
        if(mRecordRice?.yellowGrainRiceQuality == 0.00){
            yellowGrainRiceQuality.set("0.00")
        }
    }
}